package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.RoleDAO;
import cn.edu.scau.cmi.hombio.dao.RoleEmployeeDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Role;
import cn.edu.scau.cmi.hombio.domain.RoleEmployee;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for RoleEmployee entities
 * 
 */

@Service("RoleEmployeeService")

@Transactional
public class RoleEmployeeServiceImpl implements RoleEmployeeService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Role entities
	 * 
	 */
	@Autowired
	private RoleDAO roleDAO;

	/**
	 * DAO injected by Spring that manages RoleEmployee entities
	 * 
	 */
	@Autowired
	private RoleEmployeeDAO roleEmployeeDAO;

	/**
	 * Instantiates a new RoleEmployeeServiceImpl.
	 *
	 */
	public RoleEmployeeServiceImpl() {
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public RoleEmployee deleteRoleEmployeeEmployee(Integer roleemployee_id, Integer related_employee_id) {
		RoleEmployee roleemployee = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		roleemployee.setEmployee(null);
		related_employee.getRoleEmployees().remove(roleemployee);
		roleemployee = roleEmployeeDAO.store(roleemployee);
		roleEmployeeDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return roleemployee;
	}

	/**
	 * Save an existing Role entity
	 * 
	 */
	@Transactional
	public RoleEmployee saveRoleEmployeeRole(Integer id, Role related_role) {
		RoleEmployee roleemployee = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(id, -1, -1);
		Role existingrole = roleDAO.findRoleByPrimaryKey(related_role.getId());

		// copy into the existing record to preserve existing relationships
		if (existingrole != null) {
			existingrole.setId(related_role.getId());
			existingrole.setName(related_role.getName());
			existingrole.setMemo(related_role.getMemo());
			related_role = existingrole;
		}

		roleemployee.setRole(related_role);
		related_role.getRoleEmployees().add(roleemployee);
		roleemployee = roleEmployeeDAO.store(roleemployee);
		roleEmployeeDAO.flush();

		related_role = roleDAO.store(related_role);
		roleDAO.flush();

		return roleemployee;
	}

	/**
	 * Load an existing RoleEmployee entity
	 * 
	 */
	@Transactional
	public Set<RoleEmployee> loadRoleEmployees() {
		return roleEmployeeDAO.findAllRoleEmployees();
	}

	/**
	 */
	@Transactional
	public RoleEmployee findRoleEmployeeByPrimaryKey(Integer id) {
		return roleEmployeeDAO.findRoleEmployeeByPrimaryKey(id);
	}

	/**
	 * Delete an existing Role entity
	 * 
	 */
	@Transactional
	public RoleEmployee deleteRoleEmployeeRole(Integer roleemployee_id, Integer related_role_id) {
		RoleEmployee roleemployee = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee_id, -1, -1);
		Role related_role = roleDAO.findRoleByPrimaryKey(related_role_id, -1, -1);

		roleemployee.setRole(null);
		related_role.getRoleEmployees().remove(roleemployee);
		roleemployee = roleEmployeeDAO.store(roleemployee);
		roleEmployeeDAO.flush();

		related_role = roleDAO.store(related_role);
		roleDAO.flush();

		roleDAO.remove(related_role);
		roleDAO.flush();

		return roleemployee;
	}

	/**
	 * Save an existing RoleEmployee entity
	 * 
	 */
	@Transactional
	public void saveRoleEmployee(RoleEmployee roleemployee) {
		RoleEmployee existingRoleEmployee = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(roleemployee.getId());

		if (existingRoleEmployee != null) {
			if (existingRoleEmployee != roleemployee) {
				existingRoleEmployee.setId(roleemployee.getId());
				existingRoleEmployee.setMemo(roleemployee.getMemo());
			}
			roleemployee = roleEmployeeDAO.store(existingRoleEmployee);
		} else {
			roleemployee = roleEmployeeDAO.store(roleemployee);
		}
		roleEmployeeDAO.flush();
	}

	/**
	 * Delete an existing RoleEmployee entity
	 * 
	 */
	@Transactional
	public void deleteRoleEmployee(RoleEmployee roleemployee) {
		roleEmployeeDAO.remove(roleemployee);
		roleEmployeeDAO.flush();
	}

	/**
	 * Return a count of all RoleEmployee entity
	 * 
	 */
	@Transactional
	public Integer countRoleEmployees() {
		return ((Long) roleEmployeeDAO.createQuerySingleResult("select count(o) from RoleEmployee o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public RoleEmployee saveRoleEmployeeEmployee(Integer id, Employee related_employee) {
		RoleEmployee roleemployee = roleEmployeeDAO.findRoleEmployeeByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		roleemployee.setEmployee(related_employee);
		related_employee.getRoleEmployees().add(roleemployee);
		roleemployee = roleEmployeeDAO.store(roleemployee);
		roleEmployeeDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return roleemployee;
	}

	/**
	 * Return all RoleEmployee entity
	 * 
	 */
	@Transactional
	public List<RoleEmployee> findAllRoleEmployees(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<RoleEmployee>(roleEmployeeDAO.findAllRoleEmployees(startResult, maxRows));
	}
}
